package com.example.controller;

import com.example.entity.KV;
import com.example.entity.StudentAnswer;
import com.example.service.StudentAnswerService;
import com.example.service.StudentService;
import com.example.service.SubjectService;
import com.example.service.TeacherService;
import com.example.utils.RestResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author： wzz
 * @date： 2021-11-22 22:05
 */

@Controller
@RequestMapping("/content")
public class ContentController {

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentAnswerService studentAnswerService;

    @ResponseBody
    @PostMapping(value = "/option/{book_name}/{menu_topic}/{type}")
    public RestResponses compare_option(@PathVariable(value = "book_name",required =false) String book_name,
                                        @PathVariable(value = "menu_topic",required = false)String menu_topic,
                                        @PathVariable(value = "type",required =false) String type,
                                        @RequestBody KV[] arr, Authentication authentication){
        int result = 0;
        int flag = 1;

        //学号
        String studentNumber = authentication.getName();

        //题目号
        String questionNo;
        //学生答案
        String answer;
        //正确答案
        String right;
        if(type.equals("1")){
            for (int i=0; i<arr.length; i++){
                questionNo = arr[i].getName().substring(6);
                answer = arr[i].getValue();
                //学生答案
                StudentAnswer studentAnswer = new StudentAnswer();

                studentAnswer.setStudentNumber(studentNumber);
                studentAnswer.setBook_name(book_name);
                studentAnswer.setQuestionNo(questionNo);
                studentAnswer.setMenu_topic(menu_topic);
                studentAnswer.setType(type);
                studentAnswer.setStudentAnswer(answer);

                //插入
                result = studentAnswerService.insertOption(studentAnswer);
                if (result == 0){
                    return  RestResponses.fail(200,questionNo+"添加失败");
                }
                //查询本选项的正确答案
                right = subjectService.getAnswer(studentAnswer);
                studentAnswer.setAnswer(right);

                if (right.equalsIgnoreCase(answer) ){
                    studentAnswer.setScore(5);
                    studentAnswerService.updateScore(studentAnswer);
                    arr[i].setJudge("√");
                }else {
                    studentAnswer.setScore(0);
                    studentAnswerService.updateScore(studentAnswer);
                    arr[i].setJudge("×");
                }
                System.out.println(arr[i]);
            }
        }else if(type.equals("2")){
            for (int i=0; i<arr.length; i++){
                //获取首部区分名字
                if (arr[i].getName().contains("code")){
                    questionNo = arr[i].getName().substring(4);
                }else
                    questionNo = arr[i].getName().substring(6);
                answer = arr[i].getValue();

                //判断多选的话就去掉最后一个逗号
                if (answer.contains(",")){
                    answer = answer.substring(0,answer.length()-1);
                }

                //学生答案
                StudentAnswer studentAnswer = new StudentAnswer();
                studentAnswer.setStudentNumber(studentNumber);
                studentAnswer.setBook_name(book_name);
                studentAnswer.setQuestionNo(questionNo);
                studentAnswer.setMenu_topic(menu_topic);
                studentAnswer.setType(type);
                studentAnswer.setStudentAnswer(answer);

                //插入
                result = studentAnswerService.insertOption(studentAnswer);
                if (result == 0){
                    return  RestResponses.fail(200,questionNo+"添加失败");
                }
                //查询本选项的正确答案
                right = subjectService.getAnswer(studentAnswer);
                studentAnswer.setAnswer(right);
                if (right.equalsIgnoreCase(answer) ){
                    studentAnswer.setScore(5);
                    studentAnswerService.updateScore(studentAnswer);
                    arr[i].setJudge("√");
                }else {
                    studentAnswer.setScore(0);
                    studentAnswerService.updateScore(studentAnswer);
                    arr[i].setJudge("×");
                }
                System.out.println(arr[i]);
            }
        }

        return RestResponses.ok(arr,arr.length);
    }


    @ResponseBody
    @PostMapping(value = "/fillIn/{book_name}/{menu_topic}/{type}")
    public RestResponses compare_fillIn(@PathVariable(value = "book_name",required =false) String book_name,
                                        @PathVariable(value="menu_topic",required = false) String menu_topic,
                                        @PathVariable(value = "type",required =false) String type,
                                        @RequestBody KV[] arr, Authentication authentication){
        int result = 0;
        int flag = 1;

        //学号
        String studentNumber = authentication.getName();

        //题目号
        String questionNo;
        //学生答案
        String answer;

        for (int i=0; i<arr.length; i++){
            //获取首部区分名字
            if (arr[i].getName().contains("code")){
                questionNo = arr[i].getName().substring(4);
            }else
                questionNo = arr[i].getName().substring(6);
            answer = arr[i].getValue();

            //学生答案
            StudentAnswer studentAnswer = new StudentAnswer();

            studentAnswer.setStudentNumber(studentNumber);
            studentAnswer.setBook_name(book_name);
            studentAnswer.setQuestionNo(questionNo);
            studentAnswer.setMenu_topic(menu_topic);
            studentAnswer.setType(type);
            studentAnswer.setStudentAnswer(answer);

            //插入
            result = studentAnswerService.insertMu(studentAnswer);
            if (result == 0){
                return  RestResponses.fail(200,questionNo+"添加失败");
            }
        }
        return RestResponses.ok(arr,arr.length);
    }


    @ResponseBody
    @GetMapping(value = "/updateTime")
    public RestResponses studentVideo(@RequestParam("book_name") String book_name,
                                      @RequestParam("menu_topic") String menu_topic,
                                      @RequestParam("duration") Integer duration,
                                      Authentication authentication){

        if(studentAnswerService.getVideoTime(authentication.getName(),book_name,menu_topic)<duration){
            if(studentAnswerService.updateVideoDuration(authentication.getName(),book_name, menu_topic,duration)!=0)
                return RestResponses.ok();
        }
        return RestResponses.fail();

    }

    @ResponseBody
    @GetMapping(value = "/getVideoTime")
    public Integer getVideoTime(@RequestParam("book_name") String book_name,
                                @RequestParam("menu_topic") String menu_topic,
                                Authentication authentication){
        Integer time =studentAnswerService.getVideoTime(authentication.getName(),book_name,menu_topic);
        if(time==null) time=0;
        return time;
    }
}
